import asyncio
import time

import requests, aiohttp

# 同步
# url = [f'https://httpbin.org/get?age={i}' for i in range(10)]
# start = time.time()
# for ur in url:
#     res = requests.get(ur)
#     print(res.json())
# end = time.time()-start
# print(f'耗时为：{end}')


# 基础异步


# async def main():
#     await asyncio.sleep(1)
#     print("哈哈哈哈")
#     await asyncio.sleep(1)
#     print("66666")
#

# asyncio.run(main())
# print(main())


# async def tok1():
#     urls = [f"https://httpbin.org/get?age={i}" for i in range(5)]
#     for url in urls:
#         res = requests.get(url)
#         await asyncio.sleep(1)
#         print(res.json())
#
#
# async def tok2():
#     urls = [f"https://httpbin.org/get?age={i}" for i in range(5, 10)]
#     for url in urls:
#         res = requests.get(url)
#         await asyncio.sleep(1)
#         print(res.json())
#
#
# async def main():
#     await asyncio.gather(tok1(), tok2())
#
#
# if __name__ == "__main__":
#     asyncio.run(main())
async def fetch(session, url):
    async with session.get(url) as res:
        return await res.json()


async def main():
    async with aiohttp.ClientSession() as session:
        res = await asyncio.gather(
            *[fetch(session, f"https://httpbin.org/get?age={i}") for i in range(10)]
        )
        for r in res:
            print(r)


# asyncio.run(main())

import os
print(os.cpu_count())